{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding:utf-8\n",
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "def spider(url,headers):\n",
    "    with open('renming.txt', 'w', encoding='utf-8') as fp:\n",
    "        r = requests.get(url, headers=headers)\n",
    "        r.encoding = 'gb2312'\n",
    "        # test=re.findall('<li>< a href= >(.*?)</ a></li>',r.text)\n",
    "        # print(test)\n",
    "        soup = BeautifulSoup(r.text, \"html.parser\")\n",
    "        for news_list in soup.find_all(class_=\"list14\"):\n",
    "            content = news_list.text.strip()\n",
    "            fp.write(content)\n",
    "    fp.close()\n",
    "\n",
    "if __name__==\"__main__\":\n",
    "    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) '\n",
    "                             'AppleWebKit/537.36 (KHTML, like Gecko)'\n",
    "                             ' Chrome/55.0.2883.87 Safari/537.36'}  \n",
    "\n",
    "    url = 'http://www.people.com.cn/'\n",
    "    spider(url, headers)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在PyCharm中, 学习安装包 ALT+Enter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
